// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: authentication-free shared key and token derivation between clients
// feature: change E2E packet format
// feature: no authentication required between clients
// feature: provide query interfaces
// feature: develop dispatcher
// feature: Support DServer-DClient communication
// feature: Split history as an independent common module
// feature: Unified Transport Abstraction Layer Unified Transport Abstraction Layer
// feature: discovery support client and server
// ------------------------------------------------------------------

#include <discovery/participant/PDPClientListener.hpp>

#include <mutex>

#include <edds/dds/core/policy/ParameterList.hpp>
#include <elog/Log.hpp>
#include <ertps/rtps/history/ReaderHistory.h>
#include <history/ParticipantListener.h>
#include <edds/rtps/reader/RTPSReader.h>

#include <edds/rtps/builtin/data/ParticipantProxyData.h>
#include <discovery/endpoint/EDP.h>
#include <discovery/participant/PDP.h>
#include <discovery/participant/PDPEndpoints.hpp>
// #include <rtps/network/utils/external_locators.hpp>
#include <rtps/participant/RTPSParticipantImpl.h>
#include <edds/rtps/resources/TimedEvent.h>
#include <history/WriterListener.h>

using ParameterList = evbs::edds::dds::ParameterList;

namespace evbs {
namespace ertps {
namespace rtps {

PDPClientListener::PDPClientListener(PDP* parent_pdp) : PDPListener(parent_pdp) {}

bool PDPClientListener::check_discovery_conditions(ParticipantProxyData& /* participant_data */) {
    /* Do not check PID_VENDOR_ID */
    // In Discovery Server we don't impose
    // domain ids to be the same
    /* Do not check PID_DOMAIN_ID */
    /* Do not check PARTICIPANT_TYPE */
    return true;
}

PDPClientPUBListener::PDPClientPUBListener(PDPClient* in_PDP) : pdp_client_(in_PDP) {}

PDPClient* PDPClientPUBListener::pdp_client() {
    return pdp_client_;
}

void PDPClientPUBListener::onWriterChangeReceivedByAll(vbs::dispatcher::SubstrateWriter* const writer,
                                                       CacheChange_t* change) {
    (void)writer;
    (void)change;
    if (pdp_client()->connected_servers().size() < pdp_client()->remote_server_locators().size()) {
        pdp_client()->restart_sync_timer();
    }
}

} /* namespace rtps */
} /* namespace ertps */
} /* namespace evbs */
